package com.valuegroup.smarttodo.data.service;

import android.text.TextUtils;

import com.valuegroup.smarttodo.data.model.AppStatus;
import com.valuegroup.smarttodo.data.model.Ideality;
import com.valuegroup.smarttodo.data.model.IdealityDao;
import com.valuegroup.smarttodo.data.vo.IdealityItemVo;
import com.valuegroup.smarttodo.plugins.pickdialog.DateUtils;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class IdealityService extends BaseService {


    public List<IdealityItemVo> findList(boolean onlyFinish) {
        List<IdealityItemVo> result = new ArrayList<>();

        List<Ideality> list = findIdeality(onlyFinish);
        for (Ideality t : list) {
            IdealityItemVo vo = new IdealityItemVo();

            vo.setId(t.getId());
            vo.setLabTitle(t.getTitle());
            vo.setLabDate(DateUtils.date2str(t.getUpdatedDt(), DateUtils.F31));
            vo.setShowFinish(t.getIsFinish());
            vo.setCoverPath(t.getCoverPath());

            if (!vo.isShowFinish()) {
                // 成长值
                vo.setShowGrow(true);
                vo.setLabGrow((int) Math.round(t.getGrowth()));
                vo.setShowGrowDown(t.getGrowth() > 0);
            }

            result.add(vo);
        }

        return result;
    }

    private List<Ideality> findIdeality(boolean onlyFinish) {
        QueryBuilder<Ideality> q = getIdealityDao().queryBuilder();
        if (onlyFinish) {
            q.where(IdealityDao.Properties.IsFinish.eq(true));
        }
        q.orderAsc(IdealityDao.Properties.IsFinish);
        q.orderDesc(IdealityDao.Properties.UpdatedDt);
        return q.list();
    }

    public Ideality getDefaultIdeality() {
        Ideality t = new Ideality();
        t.setTitle("理想");
        t.setShortTitle("简短描述");
        t.setIsFinish(false);
        t.setFinishDt(null);
        t.setGrowth(0d);
        t.setStatus(AppStatus.Status.NORMAL);
        t.setCreatedDt(new Date());
        t.setUpdatedDt(t.getCreatedDt());
        t.setSort(0);
        return t;
    }

    public boolean existTitle(String key) {
        List<Ideality> list = getIdealityDao().queryBuilder()
                .where(IdealityDao.Properties.Title.eq(key))
                .where(IdealityDao.Properties.Status.eq(AppStatus.Status.NORMAL))
                .list();
        return list != null && list.size() > 0;
    }

    public boolean existShortTitle(String key) {
        List<Ideality> list = getIdealityDao().queryBuilder()
                .where(IdealityDao.Properties.ShortTitle.eq(key))
                .where(IdealityDao.Properties.Status.eq(AppStatus.Status.NORMAL))
                .list();
        return list != null && list.size() > 0;
    }

    public Ideality saveIdeality(Ideality target) {
        Ideality source = null;
        if (target.getId() == null || target.getId() == 0) {
            source = getDefaultIdeality();
        } else {
            source = getIdealityDao().load(target.getId());
        }
        if (source == null) {
            return null;
        }

        source.setTitle(target.getTitle());
        source.setUpdatedDt(new Date());
        getIdealityDao().save(source);
        return source;
    }

    public Ideality doFinish(Long id) {
        if (id == null) {
            return null;
        }
        Ideality t = getIdealityDao().load(id);
        if (t != null) {
            t.setIsFinish(!t.getIsFinish());
            t.setFinishDt(t.getIsFinish() ? new Date() : null);
            t.setUpdatedDt(new Date());
            getIdealityDao().save(t);
        }
        return t;
    }

    public Ideality saveImgCover(Long id, String uri) {
        Ideality t = getIdealityDao().load(id);
        if (t == null || TextUtils.isEmpty(uri)) {
            return null;
        }

        t.setCoverPath(uri);
        t.setUpdatedDt(new Date());
        getIdealityDao().save(t);
        return t;
    }


    public void delIdeality(Long id) {
        if (id == null) {
            return;
        }
        Ideality t = getIdealityDao().load(id);
        if (t == null) {
            return;
        }

        getIdealityDao().delete(t);
    }

}
